export default {

	getCookie: function(name) {
		var search = name + "=",
			offset, end;
		if (document.cookie.length > 0) {
			offset = document.cookie.indexOf(search);
			if (offset != -1) {
				offset += search.length;
				end = document.cookie.indexOf(";", offset);
				if (end == -1) {
					end = document.cookie.length;
				};
				return decodeURI(document.cookie.substring(offset, end));
			}
		}
		return "";
	},

	setCookie: function(name, value, options) {
		if (options.expires) {
			var expires = new Date();
			expires.setTime(expires.getTime() + (options.expires * 24 * 60 * 60 * 1000));
		}
		//encodeURI方法的作用是进行编码，主要防治value中有特殊字符  
		var str = name + '=' + encodeURI(value) + (expires ? '; expires=' + expires.toGMTString() : '') + (options.path ? '; path=' + options.path : '/') + (options.domain ? '; domain=' + options.domain : '');
		//+ (options.secure ? '; secure' : '');
		//+ (options.httpOnly ? '; HttpOnly' : '');
		//console.log(str);
		document.cookie = str;
	},

	delCookie: function(name, obj) {
		this.setCookie(name, "", obj);
	},

	getRootDomain: function() {
		var host = window.location.hostname;
		return host.split('.').slice(-2).join('.');
	},

	getLocalStorage: function(name) {
		return localStorage.getItem(name);
	},

	setLocalStorage: function(name, value) {
		try {
			localStorage.setItem(name, value);
		} catch (e) {
			console.log('localStorage cache to limit!');
		}
	},

	setSessionStorage: function(name, value) {
		try {
			sessionStorage.setItem(name, value);
		} catch (e) {
			console.log('sessionStorage cache to limit!');
		}
	},

	getSessionStorage: function(name) {
		return sessionStorage.getItem(name);
	},

	removeSessionStoreage: function(key) {
		sessionStorage.removeItem(key);
	},

	clearAllStroage: function(key) {
		localStorage.clear();
		sessionStorage.clear();
	},

	queryString: function(item) {
		var svalue = location.search.match(new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)", "i"));
		return svalue ? svalue[1] : svalue;
	},

	getObjSessionStorage: function(key) {
		return JSON.parse(sessionStorage.getItem(key));
	},

	getObjLocalStorage: function(key) {
		return JSON.parse(localStorage.getItem(key));
	},

	isEmail: function(email) {
		var bool = false,
			filter = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
		if (filter.test(email)) {
			bool = true;
		}
		return bool;
	},

	getBrowser: function() {
		let ua = window.navigator.userAgent,
			isIE = window.ActiveXObject != undefined && ua.indexOf("MSIE") != -1,
			isFirefox = ua.indexOf("Firefox") != -1,
			isOpera = window.opr != undefined,
			isChrome = ua.indexOf("Chrome") && window.chrome,
			isSafari = ua.indexOf("Safari") != -1 && ua.indexOf("Version") != -1;
		if (isIE) {
			return "IE";
		} else if (isFirefox) {
			return "Firefox";
		} else if (isOpera) {
			return "Opera";
		} else if (isChrome) {
			return "Chrome";
		} else if (isSafari) {
			return "Safari";
		} else {
			return "Unkown";
		}
	},

	isMobile: function() {
		var bool = true;
		if (!navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)) {
			bool = false;
		}
		return bool;
	},

	subString: function(str, length) {
		str = (str.length > length) ? str.substring(0, length) + '...' : str;
		return str;
	},


	randomWord: function(randomFlag, min, max) {
		var str = "",
			pos,
			range = min,
			arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
		if (randomFlag) {
			range = Math.round(Math.random() * (max - min)) + min;
		}
		for (var i = 0; i < range; i++) {
			pos = Math.round(Math.random() * (arr.length - 1));
			str += arr[pos];
		}
		return str;
	},

	inArray: function(value, arr) {
		if (!arr || arr.length == 0) {
			return -1;
		} else {
			for (var k in arr) {
				if (arr[k] === value) {
					return k;
				}
				if (k == arr.length - 1) {
					return -1;
				}
			}
		}

	},

	xmlToJson: function(xml) {
		var obj = {};
		if (xml.nodeType == 1) {
			if (xml.attributes.length > 0) {
				obj["@attributes"] = {};
				for (var j = 0; j < xml.attributes.length; j++) {
					var attribute = xml.attributes.item(j);
					obj["@attributes"][attribute.nodeName] = attribute.nodeValue;
				}
			}
		} else if (xml.nodeType == 3) {
			obj = xml.nodeValue;
		}
		if (xml.hasChildNodes()) {
			for (var i = 0; i < xml.childNodes.length; i++) {
				var item = xml.childNodes.item(i);
				var nodeName = item.nodeName;
				if (typeof(obj[nodeName]) == "undefined") {
					obj[nodeName] = xmlToJson(item);
				} else {
					if (typeof(obj[nodeName].length) == "undefined") {
						var old = obj[nodeName];
						obj[nodeName] = [];
						obj[nodeName].push(old);
					}
					obj[nodeName].push(xmlToJson(item));
				}
			}
		}
		return obj;
	},

	HTMLAllbrCode: function(s) {
		if (s) {
			var reg = new RegExp("\\n", "g");
			if (s.length == 0) {
				return "";
			}
			s = s.replace(reg, "<br>");
			return s;
		} else {
			return '';
		}
	},

	replaceAllSpace: function(str) {
		if (str) {
			str = str.replace(/\s+/g, "");
		}
		return str;
	},

}
